package util;

import java.io.IOException;

import org.apache.log4j.Logger;

public class VersionUpdateUtil {
	protected static Logger log = Logger.getLogger(VersionUpdateUtil.class);

	  private static String Version = "20140923";

	  public static String versionUpdateKey() { return "#" + Version + "#"; }

	  public static String dateSave(String saveString)
	  {
	    return dateSave(saveString, 1024);
	  }

	  public static String dateSave(String saveString, int clen) {
	    if ((saveString.length() > clen) && 
	      (!saveString.startsWith(versionUpdateKey())))
	      try {
	        return versionUpdateKey() + 
	          CodedUtil.encodeBase64(ZipUtil.compress(saveString));
	      } catch (IOException e) {
	        log.error(e, e);

	        return saveString;
	      }
	    return saveString;
	  }

	  public static String dateLoad(String loadString) throws Exception
	  {
	    if (loadString.startsWith(versionUpdateKey())) {
	      String parseString = loadString
	        .replaceFirst(versionUpdateKey(), "");
	      return ZipUtil.uncompress(CodedUtil.decodeBase64(parseString));
	    }
	    return loadString;
	  }
}
